﻿Imports BUS
Imports DTO
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Public Class BaoCaoTongKetHocKy

    Private Sub BaoCaoTongKetHocKy_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim _bus As New CHocKyBUS()
        Dim _danhsachhk As New List(Of CHocKyDTO)
        _danhsachhk = _bus.LayDanhSachHK()
        cbbhocky.DataSource = _danhsachhk
        cbbhocky.DisplayMember = "TenHocKy"
    End Sub

    Dim _rptbaocaohk As rptBaoCaoTongKetHocKy

    Public Sub XuLy(ByVal _giatri As Single, ByVal _strchuoi As String, ByVal _rptbaocaohk As rptBaoCaoTongKetHocKy)
        Dim _crparametervalues As New ParameterValues()
        Dim _crparameterDiscreteValue As New ParameterDiscreteValue()
        _crparameterDiscreteValue.Value = _giatri
        Dim _crparameterFieldDefinitions As ParameterFieldDefinitions
        _crparameterFieldDefinitions = _rptbaocaohk.DataDefinition.ParameterFields
        Dim _crParameterFieldDefinition As ParameterFieldDefinition
        _crParameterFieldDefinition = _crparameterFieldDefinitions(_strchuoi)
        _crparametervalues = _crParameterFieldDefinition.CurrentValues
        _crparametervalues.Clear()
        _crparametervalues.Add(_crparameterDiscreteValue)
        _crParameterFieldDefinition.ApplyCurrentValues(_crparametervalues)
    End Sub

    Private Sub btnxem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnxem.Click
        _rptbaocaohk = New rptBaoCaoTongKetHocKy()
        Dim tbcurrent As CrystalDecisions.CrystalReports.Engine.Table
        Dim tlicurrent As CrystalDecisions.Shared.TableLogOnInfo
        For Each tbcurrent In _rptbaocaohk.Database.Tables
            tlicurrent = tbcurrent.LogOnInfo
            With tlicurrent.ConnectionInfo
                .ServerName = Application.StartupPath + "\QuanLyHocSinh.mdb"
                '.UserID = ""
                '.Password = ""
                '.DatabaseName = ""
            End With
            tbcurrent.ApplyLogOnInfo(tlicurrent)
        Next tbcurrent
        Dim _HKdto As New CHocKyDTO()
        _HKdto = cbbhocky.SelectedItem
        Dim _ds As New List(Of CThamSoDTO)
        Dim _busTS As New CThamSoBUS()
        _ds = _busTS.LayDuLieuThamSo()
        XuLy(_HKdto.TenHocKy, "TenHocKy", _rptbaocaohk)        
        CrystalReportViewer1.ReportSource = _rptbaocaohk
        CrystalReportViewer1.Refresh()
    End Sub

    Private Sub btnthoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnthoat.Click
        Me.Close()
    End Sub

    Private Sub btnexport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnexport.Click
        Try
            Dim CrExportOptions As ExportOptions
            Dim CrDiskFileDestinationOptions As New DiskFileDestinationOptions()
            Dim CrFormatTypeOptions As New ExcelFormatOptions
            Dim _dlg As New SaveFileDialog()
            _dlg.Filter = "|*.xls||*.xlsx"

            If _dlg.ShowDialog() = Windows.Forms.DialogResult.OK Then
                CrDiskFileDestinationOptions.DiskFileName = _dlg.FileName
                CrExportOptions = _rptbaocaohk.ExportOptions
                With CrExportOptions
                    .ExportDestinationType = ExportDestinationType.DiskFile
                    .ExportFormatType = ExportFormatType.Excel
                    .DestinationOptions = CrDiskFileDestinationOptions
                    .FormatOptions = CrFormatTypeOptions
                End With
                _rptbaocaohk.Export()
            End If
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
End Class